package com.mall.implementation;

import java.util.ArrayList;


import com.mall.core.Item;
import com.mall.implementation.ItemImpl;
import com.mall.util.StringWrapper;

public class InventoryImpl implements com.mall.core.Inventory {

	/**
	 *This Class implements the inventory interface which specifies the responsibilities of this class.
	 *The objects of this class represents inventories for a store.
	 *A store can have one owner and one store with multiple items.
	 */
	private static final long serialVersionUID = 1L;
	
	private ArrayList<Item> items;
	private int storeid;
	private int storeownerid;
	
	public InventoryImpl(int storeid, int storeownerid){
		this.storeid=storeid;
		this.storeownerid = storeownerid;
	}
	@Override
	public boolean addaNewItemEntry(Item newitem) {
		return items.add(newitem);
		
	}

	@Override
	public boolean addNewItems(ArrayList<Item> items) {
		return items.addAll(items);
		
	}

	@Override
	public boolean FindItem(Item item) {
		boolean found=false;
		for(Item itemm : items){
			if(item.equals(itemm)){
				found=true;
			}
		}
		return found;
	}

	@Override
	public boolean FindItemById(int index) {
		boolean found=false;
		for(Item itemm : items){
			if(itemm.getId()==index){
				found=true;
			}
		}
		return found;
	}


	@Override
	public boolean deleteItem(Item item) {
		return items.remove(item);
	}

	@Override
	public boolean deleteItem(int index) {
		boolean removed=false;
	for(Item item: items){
		if(item.getId()==index){
			removed = items.remove(item);
		}

	}
	return removed;
	
	}



	@Override
	public int getItemIndex(Item item) {
		return items.indexOf(item);
	}

	@Override
	public ArrayList<StringWrapper> getItemData(Item item) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<StringWrapper> getItemData(int index) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Item getItem(int index) {
		Item returnitem = null; 
		for(Item itemm: items){
		int itemquantity = itemm.getQuantity();
		if (itemquantity!=0&&itemm.getId()==index){
			itemm.setQuantity(itemquantity-1);
			returnitem = itemm ;
		}
		else returnitem = null;
		}
		return returnitem;
	}

	@Override
	public Item getItem(Item item) {
		Item returnitem = null; 
	for(Item itemm: items){
	int itemquantity = itemm.getQuantity();
	if (itemquantity!=0&&itemm.equals(item)){
		itemm.setQuantity(itemquantity-1);
		returnitem = itemm ;
	}
	else returnitem = null;
	}
	return returnitem;
	}



	@Override
	public boolean updateItemData(Item item,StringWrapper property, StringWrapper value) {
		
		Item itemm= getItem(item);
		if(property.getModifiedString().equalsIgnoreCase("id")){
			itemm.setId(Integer.parseInt(value.getNOTModifiedString()));
		}
		if(property.getModifiedString().equalsIgnoreCase("price")){
			itemm.setItemprice(value);
		}
		if(property.getModifiedString().equalsIgnoreCase("name")){
			itemm.setItemname(value);
		}
		if(property.getModifiedString().equalsIgnoreCase("description")){
			itemm.setItemdescription(value);
		}
		if(property.getModifiedString().equalsIgnoreCase("type")){
			if(value.getModifiedString().equals("new")){
				itemm.setNeww(true);
			}
			if(value.getModifiedString().equals("refurbnished")){
				itemm.setRefurbnished(true);
			}
			if(value.getModifiedString().equals("damaged")){
				itemm.setDamaged(true);
			}
		}
		return false;
	}

	@Override
	public boolean updateItemDataById(int index,StringWrapper property, StringWrapper value) {
		
		Item itemm= getItem(index);
		if(property.getModifiedString().equalsIgnoreCase("id")){
			itemm.setId(Integer.parseInt(value.getNOTModifiedString()));
		}
		if(property.getModifiedString().equalsIgnoreCase("price")){
			itemm.setItemprice(value);
		}
		if(property.getModifiedString().equalsIgnoreCase("name")){
			itemm.setItemname(value);
		}
		if(property.getModifiedString().equalsIgnoreCase("description")){
			itemm.setItemdescription(value);
		}
		if(property.getModifiedString().equalsIgnoreCase("type")){
			if(value.getModifiedString().equals("new")){
				itemm.setNeww(true);
			}
			if(value.getModifiedString().equals("refurbnished")){
				itemm.setRefurbnished(true);
			}
			if(value.getModifiedString().equals("damaged")){
				itemm.setDamaged(true);
			}
		}
		return false;
	}

	@Override
	public int getSize() {
		return items.size();
	}

	@Override
	public void setStoreId(int storeid) {
		this.storeid=storeid;
		
	}

	@Override
	public int getStoreId() {
	return storeid;
	}
	@Override
	public int getStoreOwnerId() {

		return storeownerid;
	}
	@Override
	public void setStoreOwnerId(int storeownerid) {
		this.storeownerid =storeownerid;
		
	}


	
}
